package cn.tiga.org.controller;

import cn.tiga.basic.annotation.PreAuthorize;
import cn.tiga.basic.util.JsonResult;
import cn.tiga.basic.util.PageList;
import cn.tiga.org.domain.Employee;
import cn.tiga.org.query.EmployeeQuery;
import cn.tiga.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
public class EmployeeController {
    @Autowired
    public IEmployeeService iEmployeeService;

    /**
     * @Title: loadAll 查询全部
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 9:37
     * @Parameters: []
     * @Return java.util.List<cn.pping.org.domain.Departmant>
     */

    @GetMapping
    @PreAuthorize(name = "员工查询全部",sn = "emp:get")
    public List<Employee> loadAll() {
        return iEmployeeService.loadAll();
    }

    /**
     * @Title: loadById 根据ID查询
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 9:37
     * @Parameters: [id]
     * @Return cn.pping.org.domain.Departmant
     */

    @GetMapping("/{id}")
    @PreAuthorize(name = "员工根据id查询",sn = "emp:get")
    public Employee loadById(@PathVariable("id") Long id) {
        return iEmployeeService.loadById(id);
    }


    /**
     * @Title: romove 根据Id删除
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 10:26
     * @Parameters: [id]
     * @Return cn.pping.basic.util.JsonResult
     */

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "员工根据id删除",sn = "emp:delete")
    public JsonResult remove(@PathVariable("id") Long id) {

        try {
            iEmployeeService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            return JsonResult.me().setMsg("操作失败");

        }
    }

    /**
     * @Title: batchRomove 批量删除
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/19 19:21
     * @Parameters: [ids]
     * @Return cn.pping.basic.util.JsonResult
     */

    @PatchMapping
    @PreAuthorize(name = "员工批量删除",sn = "emp:patch")
    public JsonResult batchRomove(@RequestBody List<Long> ids) {
        try {
            iEmployeeService.batchRomove(ids);
            return JsonResult.me();
        } catch (Exception e) {
            return JsonResult.me().setMsg("操作失败");

        }
    }

    /**
     * @Title: addOrUpdate 添加修改
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 10:28
     * @Parameters: [departmant]
     * @Return cn.pping.basic.util.JsonResult
     */
    @PutMapping
    @PreAuthorize(name = "员工新增修改",sn = "emp:put")
    public JsonResult addOrUpdate(@RequestBody Employee Employee) {
        try {
            if (Employee.getId() == null) {
                iEmployeeService.add(Employee);
            } else {
                iEmployeeService.update(Employee);
            }
            return JsonResult.me();
        } catch (Exception e) {
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * @Title: queryPage 分页
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 10:34
     * @Parameters: [id]
     * @Return cn.pping.basic.util.JsonResult
     */

    @PostMapping
    @PreAuthorize(name = "员工分页查询",sn = "emp:post")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query) {
        try {
            return iEmployeeService.pageQuery(query);
        } catch (Exception e) {
            // 应为可能会报错所以前端页面返回一个空的参数 这样前端就显示数值
            return new PageList<>();
        }

    }

    /**
     * 根据shopId查询所有的店铺员工
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    @ApiOperation(value = "根据shopid查询" )
    public List<Employee> findByShopId(@PathVariable("shopId") Long shopId){
        return iEmployeeService.findByShopId(shopId);
    }


}
